package com.qianclass.talklive.app.enter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.qianclass.talklive.R;
import com.qianclass.talklive.app.activity.CourseInfoActivity;
import com.qianclass.talklive.app.activity.VideoActivity;
import com.qianclass.talklive.app.utils.Constant;
import com.qianclass.talklive.app.utils.LogUtil;
import com.qianclass.talklive.app.utils.SharedPreferencesUtils;
import com.qianclass.talklive.app.view.IBaseView;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by admin on 2017/3/8.
 * <p>
 * 更新时间 2017/3/8
 * 更新描述 ${TODO}
 */

public class IUserEnterPresenterImpl implements IUserEnterPresenter, OnEnterListener {

    private Context    mContext ;
    private IBaseView  mIBaseView;
    private EnterModel mEnterModel;
    private TextView   editText;
    public IUserEnterPresenterImpl(Context context,IBaseView mIBaseView) {
            this.mContext=context;
            this.mIBaseView=mIBaseView;
        mEnterModel = new EnterModelImpl();
    }

    @Override
    public void userEnterRoom(String RoomID, String Token,String AgencyID) {

        SharedPreferencesUtils.setParam(mContext, Constant.ENTER_CLASSROOM_TIME, System.currentTimeMillis());
        mEnterModel.userEnterCourse(RoomID,Token,AgencyID,this);

    }



    @Override
    public void showProgress() {
        mIBaseView.showProgress();
    }

    @Override
    public void hideProgress() {
        mIBaseView.hideProgress();
    }

    @Override
    public void enterSuc(final String url) {
        //这里获取到了教室协议url
        LogUtil.i("MVP","impl "+url);
        final Intent intent = new Intent(mContext, VideoActivity.class);
        intent.putExtra("urlInfo",url);

        try {
            JSONObject jsonObject = new JSONObject(url);
            if(jsonObject.has("RoomInfo")){
                JSONObject roomInfo = jsonObject.getJSONObject("RoomInfo");
                boolean needPassword = roomInfo.getBoolean("NeedPassword");
                final String password = roomInfo.getString("Password");
                if(needPassword==true){//弹出dialog

                    editText = new EditText(mContext);

                    new AlertDialog.Builder(mContext)
                            .setTitle(mContext.getResources().getString(R.string.course_enter_need_password))
                            .setIcon(android.R.drawable.ic_dialog_info)
                            .setView(editText)
                            .setPositiveButton(mContext.getResources().getString(R.string.course_sure), new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    String inputText = editText.getText().toString();
                                    if(password.equals(inputText)){//密码正确，进入教室
                                        LogUtil.i("MVP","password  "+password);
//                                        Toast.makeText(mContext,"密码正确",Toast.LENGTH_SHORT).show();
                                        mContext.startActivity(intent);

                                    }else{
                                        Toast.makeText(mContext, mContext.getResources().getString(R.string.course_enter_wrong_password),Toast.LENGTH_SHORT).show();
                                    }
                                }
                            })
                            .setNegativeButton(mContext.getResources().getString(R.string.course_cancel), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // 点击“返回”后的操作,这里不设置没有任何操作
                            CourseInfoActivity activity = (CourseInfoActivity) mContext;
                            activity.isEnterRoom = false;
                            return;
                        }
                    }).show();


                }else{//没有密码直接进入教室
                    mContext.startActivity(intent);
                }

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void enterFal(int type, String error) {
        Log.e("enterFal", error);
        /*if (error == null || TextUtils.isEmpty(error)) {
            mIBaseView.showErrorCode(mContext.getResources().getString(R.string.course_enter_course_failed));
        } else {
            mIBaseView.showErrorCode(error);
        }*/
        if (type == 0) {
            mIBaseView.showErrorCode(type, mContext.getResources().getString(R.string.course_enter_course_failed));
        } else if (type == 1) {
            mIBaseView.showErrorCode(type, error);
        }
    }
}
